package com.lyun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyun.constants.SplitConstants;
import com.lyun.mapper.VisitorMapper;
import com.lyun.pojo.User;
import com.lyun.pojo.Visitor;
import com.lyun.service.UserService;
import com.lyun.service.VisitorService;
import com.lyun.vo.VisitorsVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@DubboService
public class VisitorServiceImpl extends ServiceImpl<VisitorMapper, Visitor> implements VisitorService {

    @Resource
    private VisitorMapper visitorMapper;

    @DubboReference
    private UserService userService;

    @Override
    public List<VisitorsVo> selectVisitorsListById(Long id) {
        QueryWrapper<Visitor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("to_id", id);
        queryWrapper.orderByDesc("visit_time");
        List<Visitor> list = visitorMapper.selectList(queryWrapper);

        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        List<Long> visitorIds = list.stream().map(Visitor::getFromId).collect(Collectors.toList());
        List<User> userList = userService.getListByIds(visitorIds);

        List<VisitorsVo> voList = userList.stream().map(item -> {
            VisitorsVo vo = new VisitorsVo();
            BeanUtils.copyProperties(item, vo);
            vo.setAvatar(item.getHeader());
            String tags = item.getTags();
            if (StringUtils.hasText(tags)) {
                String[] split = tags.split(SplitConstants.SPLIT_STR);
                List<String> tagList = Arrays.asList(split);
                vo.setTags(tagList);
            }
            vo.setFateValue(60);
            return vo;
        }).collect(Collectors.toList());

        return voList;
    }

    /*@RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = EveryConstant.TANHUA_VISITORQUEUE),
            exchange = @Exchange(
                    name = EveryConstant.TANHUA_DEFAULTEXCHANGE
            ),
            key = {EveryConstant.TANHUA_VISITORKEY}
    ))
    public void insertVisitor(RabbitVisitorDto dto) {

        Visitor visitor = new Visitor();
        BeanUtils.copyProperties(dto, visitor);
        visitor.setVisitTime(LocalDateTime.now());

    }*/

}
